package com.fingard.dsp.bank.directbank.icbc02;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class ICBC02Deposit extends ICBC02Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            bnkPackageID = p_req7507.reqHeader.transSeqID;
            bnkTransCode = "QFIXLIST";
            bnkfSeqno = bnkPackageID;
            bnkVersion = "0.0.1.0";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入ICBC02通用的头部节点
            writeCommonHeader(xmlWriter);
            xmlWriter.writeElementString("AccNo", p_req7507.actNum);
            xmlWriter.writeElementString("CurrType", convertCurToBank(p_req7507.actCur));
            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(new Date());
            tmpCalendarFrom.add(Calendar.YEAR, -5);
            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(new Date());
            xmlWriter.writeElementString("BeginDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));
            xmlWriter.writeElementString("EndDate", Format.Date8Format.format(tmpCalendarTo.getTime()));
            xmlWriter.writeElementClosed("ReqReserved1");
            xmlWriter.writeElementClosed("ReqReserved2");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            XmlTextReader tmpBkReader;
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                    if (isActiveRetCode(tmpRetCode)) {
                        while (tmpBkReader.read("/CMS/eb/out")) {
                            XElement readXElement = tmpBkReader.readedXElement;
                            if (readXElement.getSingleTextNotNull("VouhNo").indexOf("861") == 0) {
                                continue;
                            }
                            RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                            retAtsDetail.actNum = p_req7507.actNum;//银行账号
                            retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                            retAtsDetail.currencyID = convertCurToATS(readXElement.getSingleTextNotNull("CurrType"));//币种
                            retAtsDetail.depositCode = readXElement.getSingleTextNotNull("VouhNo");//证实书号
                            /* 状态
                            0-正常
                            1-账户冻结
                            2-未启用
                            3-退票
                            4-销户
                            6-挂失
                            7-冻结挂失
                            8-作废*/
                            String tmpDepositStates = readXElement.getSingleTextNotNull("Status");
                            String depositStates = "1";
                            if (tmpDepositStates.equals("0")) {
                                depositStates = "1";
                            } else if (tmpDepositStates.equals("1")) {
                                depositStates = "2";
                            } else if (tmpDepositStates.equals("2")) {
                                depositStates = "3";
                            } else if (tmpDepositStates.equals("3")) {
                                depositStates = "4";
                            } else if (tmpDepositStates.equals("4")) {
                                depositStates = "5";
                            } else if (tmpDepositStates.equals("6") || tmpDepositStates.equals("7")) {
                                depositStates = "7";
                            } else if (tmpDepositStates.equals("8")) {
                                depositStates = "9";
                            }
                            retAtsDetail.depositStates = depositStates;
                            retAtsDetail.startDate = readXElement.getSingleTextNotNull("OpenDate");//开户日
                            retAtsDetail.valueDate = readXElement.getSingleTextNotNull("ValueDay");//起息日
                            String terms = "9";
                            if (p_req7507.savingType.equals("4")) {
                                /*
                                通知种类
                                通知存款专用字段
                                1：一天通知存款
                                7：七天通知存款
                                 */
                                String advisef = readXElement.getSingleTextNotNull("Advisef");
                                if (advisef.equals("1")) {
                                    terms = "0";
                                } else if (advisef.equals("7")) {
                                    terms = "1";
                                }
                            } else {
                                /*
                                存期
                                单位定期专用字段。
                                不足一个月：9＋两位的天数，比如907，表示7天
                                大于一个月：用小于900的整数作为月份数
                                如下是例子：
                                907-1周 914-2周 921-3周 1-一个月 2-二个月 3-三个月 以此类推
                                 */
                                String saveTerm = readXElement.getSingleTextNotNull("SaveTerm");
                                if (saveTerm.equals("907")) {
                                    terms = "1";
                                } else if (saveTerm.equals("1")) {
                                    terms = "2";
                                } else if (saveTerm.equals("3")) {
                                    terms = "3";
                                } else if (saveTerm.equals("6")) {
                                    terms = "4";
                                } else if (saveTerm.equals("12")) {
                                    terms = "5";
                                } else if (saveTerm.equals("24")) {
                                    terms = "6";
                                } else if (saveTerm.equals("36")) {
                                    terms = "7";
                                } else if (saveTerm.equals("60")) {
                                    terms = "8";
                                }
                            }
                            retAtsDetail.terms = terms;
                            retAtsDetail.rate = String.valueOf(Double.valueOf(readXElement.getSingleTextNotNull("Rate")) / 1000000);//利率 rate/100000000  * 100%(例如返回1000000就代表0.01的利息，相当于1%)
                            retAtsDetail.noteMoney = StringHelper.amountDivide100(readXElement.getSingleTextNotNull("Amount"));//金额
                            retAtsDetail.endDate = readXElement.getSingleTextNotNull("CloseDate");//到期日
                            retAts.writeRetDetail(retAtsDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
